;(function() {
	Nokia.TemplateDefault = Nokia.Template.extend({
		footerIconicMenu: null,

		initialize: function(options) {
			var instance = this;

			var defaults = {
				showFirstView: true
			};

			instance.options = jQuery.extend(defaults, options);

			instance._super.apply(instance, [instance.options]);

			instance.viewManager = options.viewManager;

			if (!instance.options.showSoftKeys && window.menu) {
				menu.hideSoftkeys();
			}

			if (instance.options.header) {

				instance.setHeader(
					Nokia.dom.parseHTML('<div class="nokia-template-header"></div>')
				);

				instance.options.header.element = instance.getHeader();

				instance.headerTopBar = new Nokia.TopBar(
					instance.options.header
				);

				Nokia.dom.append(instance.getHeader(), instance.headerTopBar.getWrapper());
				Nokia.dom.append(instance.getContainer(), instance.getHeader());
			}

			instance.setContent(
				Nokia.dom.parseHTML('<div class="nokia-template-content"></div>')
			);

			Nokia.dom.append(instance.getContainer(), instance.getContent());

			if (instance.options.footer) {

				instance.setFooter(
					Nokia.dom.parseHTML('<div class="nokia-template-footer"></div>')
				);

				instance.options.footer.element = Nokia.dom.parseHTML('<div></div>');

				instance.footerIconicMenu = new Nokia.IconicMenu(
					instance.options.footer
				);

				Nokia.dom.append(instance.getFooter(), instance.footerIconicMenu.getWrapper());
				Nokia.dom.append(instance.getContainer(), instance.getFooter());

			} else {

				instance.getContent().addClass('nokia-template-content-no-footer');

			}

			// Append views to the template content
			instance.views = instance.viewManager.getViews();

			jQuery.each(instance.views, function(i, view) {
				view.setTemplate(instance);
				view._renderUI(instance.getContent());
			});

			instance.viewManager.hideAll();

			// Show first view
			if (instance.options.showFirstView) {
				instance.viewManager.show(instance.views[0]);
			}

		},

		setSoftKeysItems: function(items) {
			var instance = this;

			if (window.menu) {

				menu.clear();

				jQuery.each(items, function(i, item) {
					var menuItem = new MenuItem(item.label, i);
					menuItem.onSelect = item.callback;

					menu.append(menuItem);
				});

			}

		},

		getViewManager: function() {
			var instance = this;

			return instance.viewManager;
		},

		getHeaderTopBar: function() {
			var instance = this;

			return instance.headerTopBar;
		},

		getFooterIconicMenu: function() {
			var instance = this;

			return instance.footerIconicMenu;
		}

	});
})();